<template>
	<view class="u-select">
		<!-- <view class="u-select__action" :class="{
			'u-select--border': border
		}" @tap.stop="selectHandler">
			<view class="u-select__action__icon" :class="{
				'u-select__action__icon--reverse': value == true
			}">
				<u-icon name="arrow-down-fill" size="26" color="#c0c4cc"></u-icon>
			</view>
		</view> -->
		<u-popup :maskCloseAble="maskCloseAble" mode="bottom" :popup="false" v-model="value" length="auto" :safeAreaInsetBottom="safeAreaInsetBottom" @close="close" :z-index="uZIndex">
			<view class="u-select">
				<view class="u-select__header" @touchmove.stop.prevent="">
					<view
						class="u-select__header__cancel u-select__header__btn"
						:style="{ color: cancelColor }"
						hover-class="u-hover-class"
						:hover-stay-time="150"
						@tap="getResult('cancel')"
					>
						{{cancelText}}
					</view>
					<view class="u-select__header__title">
						{{title}}
					</view>
					<view
						class="u-select__header__confirm u-select__header__btn"
						:style="{ color: moving ? cancelColor : confirmColor }"
						hover-class="u-hover-class"
						:hover-stay-time="150"
						@touchmove.stop=""
						@tap.stop="getResult('confirm')"
					>
						{{confirmText}}
					</view>
				</view>
				<view class="u-select__body">
					<picker-view @change="columnChange" class="u-select__body__picker-view" :value="defaultSelector" @pickstart="pickstart" @pickend="pickend">
						<picker-view-column v-for="(item, index) in columnData" :key="index">
							<view class="u-select__body__picker-view__item" v-for="(item1, index1) in item" :key="index1">
								<view class="u-line-1">{{ item1[labelName] }}</view>
							</view>
						</picker-view-column>
					</picker-view>
				</view>
			</view>
		</u-popup>
	</view>
</template>

<script>
	

export default {
	props: {
		
		list: {
			type: Array,
			default() {
				return [];
			}
		},
		
		border: {
			type: Boolean,
			default: true
		},
		
		value: {
			type: Boolean,
			default: false
		},
		
		cancelColor: {
			type: String,
			default: '#606266'
		},
		
		confirmColor: {
			type: String,
			default: '#2979ff'
		},
		
		zIndex: {
			type: [String, Number],
			default: 0
		},
		safeAreaInsetBottom: {
			type: Boolean,
			default: false
		},
		
		maskCloseAble: {
			type: Boolean,
			default: true
		},
		
		defaultValue: {
			type: Array,
			default() {
				return [0];
			}
		},
		
		mode: {
			type: String,
			default: 'single-column'
		},
		
		valueName: {
			type: String,
			default: 'value'
		},
		
		labelName: {
			type: String,
			default: 'label'
		},
		
		childName: {
			type: String,
			default: 'children'
		},
		
		title: {
			type: String,
			default: ''
		},
		
		cancelText: {
			type: String,
			default: '取消'
		},
		
		confirmText: {
			type: String,
			default: '确认'
		}
	},
	data() {
		return {
			
			defaultSelector: [0],
			
			columnData: [],
			
			selectValue: [],
			
			lastSelectIndex: [],
			
			columnNum: 0,
			
			moving: false
		};
	},
	watch: {
		
		value: {
			immediate: true,
			handler(val) {
				if(val) setTimeout(() => this.init(), 10);
			}
		},
	},
	computed: {
		uZIndex() {
			
			return this.zIndex ? this.zIndex : this.$u.zIndex.popup;
		},
	},
	methods: {
		
		pickstart() {
			// #ifdef MP-WEIXIN
			this.moving = true;
			// #endif
		},
		
		pickend() {
			// #ifdef MP-WEIXIN
			this.moving = false;
			// #endif
		},
		init() {
			this.setColumnNum();
			this.setDefaultSelector();
			this.setColumnData();
			this.setSelectValue();
		},
		
		setDefaultSelector() {
			
			this.defaultSelector = this.defaultValue.length == this.columnNum ? this.defaultValue : Array(this.columnNum).fill(0);
			this.lastSelectIndex = this.$u.deepClone(this.defaultSelector);
		},
		
		setColumnNum() {
			
			if(this.mode == 'single-column') this.columnNum = 1;
			
			else if(this.mode == 'mutil-column') this.columnNum = this.list.length;
			
			else if(this.mode == 'mutil-column-auto') {
				let num = 1;
				let column = this.list;
				
				while(column[0][this.childName]) {
					column = column[0] ? column[0][this.childName] : {};
					num ++;
				}
				this.columnNum = num;
			}
		},
		
		setColumnData() {
			let data = [];
			this.selectValue = [];
			if(this.mode == 'mutil-column-auto') {
				
				let column = this.list[this.defaultSelector.length ? this.defaultSelector[0] : 0];
				
				for (let i = 0; i < this.columnNum; i++) {
					
					if (i == 0) {
						data[i] = this.list;
						column = column[this.childName];
					} else {
						
						data[i] = column;
						column = column[this.defaultSelector[i]][this.childName];
					}
				}
			} else if(this.mode == 'single-column') {
				data[0] = this.list;
			} else {
				data = this.list;
			}
			this.columnData = data;
		},
		
		setSelectValue() {
			let tmp = null;
			for(let i = 0; i < this.columnNum; i++) {
				tmp = this.columnData[i][this.defaultSelector[i]];
				let data = {
					value: tmp ? tmp[this.valueName] : null,
					label: tmp ? tmp[this.labelName] : null
				};
				
				if(tmp && tmp.extra) data.extra = tmp.extra;
				this.selectValue.push(data)
			}
		},
		
		columnChange(e) {
			let index = null;
			let columnIndex = e.detail.value;
			
			this.selectValue = [];
			if(this.mode == 'mutil-column-auto') {
				
				this.lastSelectIndex.map((val, idx) => {
					if (val != columnIndex[idx]) index = idx;
				});
				this.defaultSelector = columnIndex;
				for (let i = index + 1; i < this.columnNum; i++) {
					
					
					this.columnData[i] = this.columnData[i - 1][i - 1 == index ? columnIndex[index] : 0][this.childName];
					
					this.defaultSelector[i] = 0;
				}
				
				
				columnIndex.map((item, index) => {
					let data = this.columnData[index][columnIndex[index]];
					let tmp = {
						value: data ? data[this.valueName] : null,
						label: data ? data[this.labelName] : null,
					};
					
					if(data && data.extra !== undefined) tmp.extra = data.extra;
					this.selectValue.push(tmp);

				})
				
				this.lastSelectIndex = columnIndex;
			} else if(this.mode == 'single-column') {
				let data = this.columnData[0][columnIndex[0]];
				
				let tmp = {
					value: data ? data[this.valueName] : null,
					label: data ? data[this.labelName] : null,
				};
				
				if(data && data.extra !== undefined) tmp.extra = data.extra;
				this.selectValue.push(tmp);
			} else if(this.mode == 'mutil-column') {
				
				columnIndex.map((item, index) => {
					let data = this.columnData[index][columnIndex[index]];
					
					let tmp = {
						value: data ? data[this.valueName] : null,
						label: data ? data[this.labelName] : null,
					};
					
					if(data && data.extra !== undefined) tmp.extra = data.extra;
					this.selectValue.push(tmp);
				})
			}
		},
		close() {
			this.$emit('input', false);
		},
		
		getResult(event = null) {
			// #ifdef MP-WEIXIN
			if (this.moving) return;
			// #endif
			if (event) this.$emit(event, this.selectValue);
			this.close();
		},
		selectHandler() {
			this.$emit('click');
		}
	}
};
</script>

<style scoped lang="scss">
@import "../../libs/css/style.components.scss";

.u-select {

	&__action {
		position: relative;
		line-height: $u-form-item-height;
		height: $u-form-item-height;

		&__icon {
			position: absolute;
			right: 20rpx;
			top: 50%;
			transition: transform .4s;
			transform: translateY(-50%);
			z-index: 1;

			&--reverse {
				transform: rotate(-180deg) translateY(50%);
			}
		}
	}

	&__hader {
		&__title {
			color: $u-content-color;
		}
	}

	&--border {
		border-radius: 6rpx;
		border-radius: 4px;
		border: 1px solid $u-form-item-border-color;
	}

	&__header {
		@include vue-flex;
		align-items: center;
		justify-content: space-between;
		height: 80rpx;
		padding: 0 40rpx;
	}

	&__body {
		width: 100%;
		height: 500rpx;
		overflow: hidden;
		background-color: #fff;

		&__picker-view {
			height: 100%;
			box-sizing: border-box;

			&__item {
				@include vue-flex;
				align-items: center;
				justify-content: center;
				font-size: 32rpx;
				color: $u-main-color;
				padding: 0 8rpx;
			}
		}
	}
}
</style>
